﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Formatting;
using System.Runtime.Serialization.Json;
using System.Web.Http;
using System.Web.Http.ModelBinding;
using Cofe.Core;
using Cofe.IO;
using Cofe.Web.Utils;
using Newtonsoft.Json.Serialization;

namespace Cofe.Web
{
    public static class WebApiConfig
    {        
        public static void Register(HttpConfiguration config)
        {

            //var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
            //config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
            

            //var xmlToJsonTypes = CofeServices.PropertyInterfaceManager.AllCastableContainers.ToList();
            //xmlToJsonTypes.Add(typeof(WebEntryList));
            //config.Formatters.Insert(0, new XmlToJsonSerializerFormatter(xmlToJsonTypes.ToArray()));

            config.Formatters.Clear();
            config.Formatters.Add(new JsonMediaTypeFormatter());
            config.Formatters.Add(new JQueryMvcFormUrlEncodedFormatter());
            config.Formatters.Add(new SyndicationFeedFormatter());
            //config.Formatters.Add(new XmlMediaTypeFormatter());
            

            var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
            json.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
            json.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
            json.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
           
            config.Routes.MapHttpRoute(
                name: "DefaultEntryApi",
                routeTemplate: "api/{controller}/{id}/{resource}",
                defaults: new {  
                    controller = "entry",
                    resource = RouteParameter.Optional                    
                }, constraints: new { id = new GuidConstraint() }
            );

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{resource}",
                defaults: new
                {
                    resource = RouteParameter.Optional
                }
            );

            config.Routes.MapHttpRoute(
                name: "DefaultEventsApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new
                {
                    controller = "events"                    
                }
                , constraints: new { id = new GuidConstraint() }
            );

            // Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable<T> return type.
            // To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries.
            // For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712.
            //config.EnableQuerySupport();
        }
    }
}